package com.mxnet.chongjin.supoman.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.mxnet.chongjin.supoman.config.Api;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * author: Administrator
 * email: lcj270018655@163.com
 * date: 2018/5/4
 */

public class UDPUtils {
    public static void sendUDP(byte[] b, String ip, int port) {
        //        byte[] sendBuf = msg.getBytes();
        byte[] sendBuf = b;
        try {
            // 发送
            DatagramSocket sendSocket = new DatagramSocket(port);
            InetAddress broadcastAddress = InetAddress.getByName(ip);
            DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, broadcastAddress, port);
            sendSocket.send(sendPacket);
            //            sendSocket.close();

            // 接收
            byte[] receiveBuf = new byte[20];
            DatagramPacket receivePacket = new DatagramPacket(receiveBuf, receiveBuf.length);
            sendSocket.receive(receivePacket);
            // 获得Wifi模块回复的自身IP
            InetAddress address = receivePacket.getAddress();
            String reIp = address.getHostAddress();
            byte[] data = receivePacket.getData();
            String str = StringUtils.asciiByte2Str(data);
            LoggerUtils.i("*******==sendUDP==", reIp + "\n" + str);
            sendSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static DatagramSocket mDatagramSocket = null;

    private static DatagramSocket getDatagramSocket() {
        if (mDatagramSocket == null) {
            try {
                mDatagramSocket = new DatagramSocket(7682);
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
        return mDatagramSocket;
    }

    public static void receiveUDP(Context context) {
        try {
            DatagramSocket receiveSocket = getDatagramSocket();
            while (true) {
                byte[] b = new byte[80];
                DatagramPacket packet = new DatagramPacket(b, b.length);
                receiveSocket.receive(packet);
                //对收到的数据处理
                String resIp = packet.getAddress().getHostAddress();

                byte[] data = packet.getData();
                int length = packet.getLength();
                byte[] da = new byte[length];
                for (int m = 0; m < length; m++) {
                    da[m] = data[m];
                }
                String dataStr = StringUtils.asciiByte2Str(da);
                if (!resIp.equals(WifiUtils.getIpAddress(context))) {
                    int i = dataStr.indexOf(":", 15);
                    String mac = dataStr.substring(i + 1, dataStr.length());
                    String[] s = mac.split(",");
                    StringBuffer sb = new StringBuffer();
                    for (int k = 0; k < s.length; k++) {
                        String str = Integer.toHexString(Integer.parseInt(s[k]));
                        if (str.length() == 1) {
                            str = "0" + str;
                        }
                        sb.append(str);
                    }
                    SharedPreferences sp = context.getSharedPreferences("supoman", Context.MODE_PRIVATE);
                    sp.edit().putString("MAC", sb.toString().trim()).commit();
                    LoggerUtils.i("*****==mac==", sb.toString().trim());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendUDPBroadcast() {
        // 116.62.208.115    74.3E.D0.73
        // 116.62.203.85     74.3E.CB.55
        //
        byte[] ipByte=ByteArrayUtils.ipToHex(Api.SERVER_IP);

        byte[] b = new byte[]{(byte) 0x69, (byte) 0x70, ipByte[0], ipByte[1], ipByte[2], ipByte[3]};
        try {
            DatagramSocket ms = getDatagramSocket();
            InetAddress address = InetAddress.getByName("255.255.255.255");
            DatagramPacket packet = new DatagramPacket(b, b.length, address, 7682);
            ms.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

